package com.ssyc.gsk_master.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.SPUtil;
import com.ssyc.gsk_master.R;
import com.ssyc.gsk_master.adapter.DiyRedpackageAdapter;
import com.ssyc.gsk_master.bean.DiyRedpackageInfo;
import com.ssyc.gsk_master.bean.RedPackageInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 红包定制方案
 */
public class MasterDiyRedPackageActivity extends Activity implements
		OnClickListener {

	private ListView lv_diy;

	private DiyRedpackageAdapter mAdapter;

	private List<RedPackageInfo> mDatas;

	private TextView tv_addred;

	private LinearLayout tv_cancel;

	private Button bt_creat;

	private RedPackageInfo info;

	private AlertDialog alertDialog;

	private Window window;

	private int dialogWidth = 550;

	private int dialogHeight = 260;

	private WindowManager.LayoutParams lp;

	private TextView dialog_creat;

	private TextView dialog_cancel;

	private Display mDisplay;

	private EditText et_money;

	private EditText et_number;

	private RelativeLayout rl_null;

	private TextView tv_moneys;

	private TextView tv_count;

	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.master_activity_diy_redpackage);
//		Res.init(this);

		// 获取屏幕尺寸
		getPhoneWindow();

		// 设置状态栏
		setBar();

		// 初始化View
		initView();

		// 设置数据
		initdata();

		// 设置监听
		initListener();

	}

	/**
	 * 获取屏幕尺寸
	 *
	 */
	private void getPhoneWindow() {

		mDisplay = getWindowManager().getDefaultDisplay();

		int W = mDisplay.getWidth();

		int H = mDisplay.getHeight();

		dialogWidth = W * 3 / 4;

		dialogHeight = dialogWidth *3 / 4;

	}

	/**
	 * 初始化监听
	 */
	private void initListener() {

		tv_cancel.setOnClickListener(this);

		tv_addred.setOnClickListener(this);

		bt_creat.setOnClickListener(this);

	}

	/**
	 * 初始化View的方法
	 */
	private void initView() {

		tv_cancel =  findViewById(R.id.tv_cancel);

		lv_diy =  findViewById(R.id.lv_diy);

		tv_addred =  findViewById(R.id.tv_addred);

		tv_moneys =  findViewById(R.id.tv_moneys);

		tv_count = findViewById(R.id.tv_count);

		bt_creat =  findViewById(R.id.bt_creat);

		rl_null = findViewById(R.id.rl_null);

	}

	/**
	 * 初始化数据
	 */
	private void initdata() {

		mDatas = new ArrayList<RedPackageInfo>();

		String diyJson = getIntent().getStringExtra("diyJson");

		if(diyJson!=null&&!TextUtils.isEmpty(diyJson)){
			DiyRedpackageInfo info = new DiyRedpackageInfo();
			info = GsonUtil.jsonToBean(diyJson, DiyRedpackageInfo.class);
			mDatas = info.list;

		}


		mAdapter = new DiyRedpackageAdapter(this, mDatas,
				R.layout.master_item_diy_redpackage);

		lv_diy.setAdapter(mAdapter);

		// 长按删除方案条目
		lv_diy.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					final int position, long id) {
				// TODO Auto-generated method stub

				final NormalDialog dialog = new NormalDialog(
						MasterDiyRedPackageActivity.this);

				dialog.content("是否删除该定制方案").btnText("取消", "确定")//
						.style(NormalDialog.STYLE_ONE)//
						.titleTextSize(23)//
						.show();

				dialog.setCanceledOnTouchOutside(false);

				dialog.setOnBtnClickL(new OnBtnClickL() {
					@Override
					public void onBtnClick() {
						dialog.dismiss();
					}
				}, new OnBtnClickL() {
					@Override
					public void onBtnClick() {

						mDatas.remove(position);

						mAdapter.updateListView(mDatas);

						if (mDatas.size() == 0) {

							lv_diy.setVisibility(lv_diy.GONE);

							rl_null.setVisibility(rl_null.VISIBLE);

						} else {

							lv_diy.setVisibility(lv_diy.VISIBLE);

							rl_null.setVisibility(rl_null.GONE);

						}

						int moneys = 0;

						int count = 0;

						// 计算总金额及总个数
						for (int i = 0; i < mDatas.size(); i++) {

							moneys = moneys
									+ (mDatas.get(i).money * mDatas.get(i).number);

							count = count + mDatas.get(i).number;
						}

						tv_moneys.setText("红包总金额：   " + moneys+" 元");

						tv_count.setText(count + " 个");

						dialog.dismiss();
					}
				});

//				dialog.setOnBtnLeftClickL(new OnBtnLeftClickL() {
//
//					@Override
//					public void onBtnLeftClick() {
//
//						dialog.dismiss();
//
//					}
//				});
//
//				dialog.setOnBtnRightClickL(new OnBtnRightClickL() {
//
//					@Override
//					public void onBtnRightClick() {
//
//						mDatas.remove(position);
//
//						mAdapter.updateListView(mDatas);
//
//						if (mDatas.size() == 0) {
//
//							lv_diy.setVisibility(lv_diy.GONE);
//
//							rl_null.setVisibility(rl_null.VISIBLE);
//
//						} else {
//
//							lv_diy.setVisibility(lv_diy.VISIBLE);
//
//							rl_null.setVisibility(rl_null.GONE);
//
//						}
//
//						int moneys = 0;
//
//						int count = 0;
//
//						// 计算总金额及总个数
//						for (int i = 0; i < mDatas.size(); i++) {
//
//							moneys = moneys
//									+ (mDatas.get(i).money * mDatas.get(i).number);
//
//							count = count + mDatas.get(i).number;
//						}
//
//						tv_moneys.setText("红包总金额：   " + moneys+" 元");
//
//						tv_count.setText(count + " 个");
//
//						dialog.dismiss();
//					}
//				});
				return false;
			}
		});

		if (mDatas.size() == 0) {

			lv_diy.setVisibility(lv_diy.GONE);

			rl_null.setVisibility(rl_null.VISIBLE);

		} else {

			lv_diy.setVisibility(lv_diy.VISIBLE);

			rl_null.setVisibility(rl_null.GONE);

			int moneys = 0;

			int count = 0;

			for (int i = 0; i < mDatas.size(); i++) {

				moneys = moneys + (mDatas.get(i).money * mDatas.get(i).number);

				count = count + mDatas.get(i).number;
			}

			tv_moneys.setText("红包总金额：" + moneys+"元");

			tv_count.setText(count + " 个");

		}
	}

	/**
	 * 页面控件点击事件
	 *
	 * @param view
	 */
	@Override
	public void onClick(View view) {

	    if(view.getId()==R.id.tv_addred){
            if (mDatas.size() > 25) {
                Toast.makeText(MasterDiyRedPackageActivity.this, "方案数最大为26",
                        Toast.LENGTH_SHORT).show();
                return;
            }
            // 添加红包对话框
            addRedpackage();
        }else if(view.getId()==R.id.tv_cancel){

			// 返回
			DiyRedpackageInfo info = new DiyRedpackageInfo();
			info.list = mDatas;

			String diyJson = GsonUtil.objectToJson(info);

			Intent intent = new Intent(MasterDiyRedPackageActivity.this, MasterCreatRedPackageActivity.class);

			SPUtil.put(this,"diyJson", diyJson);
			startActivity(intent);
			finish();
		}else if(view.getId()==R.id.bt_creat){
			// 确定
			DiyRedpackageInfo info1 = new DiyRedpackageInfo();
			info1.list = mDatas;

			String diyJson1 = GsonUtil.objectToJson(info1);

			Intent intent1 = new Intent(MasterDiyRedPackageActivity.this, MasterCreatRedPackageActivity.class);

			SPUtil.put(this,"diyJson",diyJson1);
			startActivity(intent1);

			finish();
		}







	}

	/**
	 * 添加红包弹窗
	 *
	 */
	public void addRedpackage() {

		alertDialog = new AlertDialog.Builder(this).create();

		alertDialog.show();

		window = alertDialog.getWindow();

		lp = window.getAttributes();

		lp.x = 0;

		window.setContentView(R.layout.master_dialog_diy_redpackage);

		alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable());

		alertDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

		window.setLayout(dialogWidth, dialogHeight);

		dialog_cancel = (TextView) alertDialog.getWindow().findViewById(
				R.id.tv_cancel);

		dialog_creat = (TextView) alertDialog.getWindow().findViewById(
				R.id.tv_creat);

		et_money = (EditText) alertDialog.getWindow().findViewById(
				R.id.et_money);

		et_number = (EditText) alertDialog.getWindow().findViewById(
				R.id.et_number);

		// 取消键
		dialog_cancel.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				alertDialog.dismiss();
			}
		});

		// 添加键
		dialog_creat.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (TextUtils.isEmpty(et_money.getText().toString().trim())
						|| "0".equals(et_money.getText().toString().trim())) {

					Toast.makeText(MasterDiyRedPackageActivity.this, "请输入红包金额",
							Toast.LENGTH_SHORT).show();

					return;
				}

				if (TextUtils.isEmpty(et_number.getText().toString().trim())
						|| "0".equals(et_number.getText().toString().trim())) {

					Toast.makeText(MasterDiyRedPackageActivity.this, "请输入红包个数",
							Toast.LENGTH_SHORT).show();

					return;
				}

				int money = Integer.parseInt(et_money.getText().toString()
						.trim());

				int number = Integer.parseInt(et_number.getText().toString()
						.trim());

				if (money == 0 || number == 0) {

					Toast.makeText(MasterDiyRedPackageActivity.this, "请先完善红包方案",
							Toast.LENGTH_SHORT).show();

					return;
				}

				int tempMoneys = 0;

				int tempnumbers = 0;

				for (int i = 0; i < mDatas.size(); i++) {

					tempMoneys = tempMoneys
							+ (mDatas.get(i).money * mDatas.get(i).number);

					tempnumbers = tempnumbers + mDatas.get(i).number;
				}

				tempMoneys = tempMoneys + (money * number);

				tempnumbers = tempnumbers + number;

				if (tempMoneys > 100000000 || (money * number) < 0) {
					Toast.makeText(MasterDiyRedPackageActivity.this, "总金额不能超过1亿",
							Toast.LENGTH_SHORT).show();

					return;
				}
				if (tempnumbers>9999) {
					Toast.makeText(MasterDiyRedPackageActivity.this, "红包总个数超限",
							Toast.LENGTH_SHORT).show();

					return;
				}

				info = new RedPackageInfo(money, number);

				mDatas.add(info);

				if (mDatas.size() == 0) {

					lv_diy.setVisibility(lv_diy.GONE);

					rl_null.setVisibility(rl_null.VISIBLE);

				} else {

					lv_diy.setVisibility(lv_diy.VISIBLE);

					rl_null.setVisibility(rl_null.GONE);

				}

				mAdapter.updateListView(mDatas);

				int moneys = 0;

				int count = 0;

				for (int i = 0; i < mDatas.size(); i++) {
					moneys = moneys
							+ (mDatas.get(i).money * mDatas.get(i).number);

					count = count + mDatas.get(i).number;
				}

				tv_moneys.setText("红包总金额：   " + moneys+" 元");

				tv_count.setText(count + " 个");

				alertDialog.dismiss();
			}
		});
	}

	/**
	 * 设置沉浸状态栏的方法,需要在setContentView()之后调用
	 */
	private void setBar() {
		// 兼容5.0+，状态栏设置透明
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
			Window window = getWindow();
			window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
			window.getDecorView().setSystemUiVisibility(
					View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
							| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
			window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
			window.setStatusBarColor(Color.TRANSPARENT);
		} else
		// 为了沉浸式状态栏 4.4以上才有
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			getWindow().addFlags(
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
		}
	}
}
